/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/8/25 14:30
@File     : update.go
@Project  : web_scaff
@Desc     :
*/
package user

import (
	daoUser "web_scaff/dao/user"
	"web_scaff/models/response"
	sysValidator "web_scaff/pkg/validator"
	serviceUser "web_scaff/service/user"

	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

type reqData struct {
	Id string `json:"id" uri:"id" binding:"required"`
}

func Update(c *gin.Context) {
	var req = new(reqData)
	var reqBody = new(daoUser.ReqBody)
	if err := c.ShouldBindUri(req); err != nil {
		// 请求参数有误，直接返回响应
		zap.L().Error("更新用户失败", zap.Error(err))
		// 判断err是不是validator.ValidationErrors 类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMsg("更新用户失败", c)
			return
		}
		response.FailWithMsg(sysValidator.RemoveTopStruct(errs.Translate(sysValidator.Trans)), c)
		return
	}
	if err := c.ShouldBind(reqBody); err != nil {
		// 请求参数有误，直接返回响应
		zap.L().Error("更新用户失败", zap.Error(err))
		// 判断err是不是validator.ValidationErrors 类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMsg("更新用户失败", c)
			return
		}
		response.FailWithMsg(sysValidator.RemoveTopStruct(errs.Translate(sysValidator.Trans)), c)
		return
	}
	if err := serviceUser.Update(req.Id, reqBody); err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.Success(c)
}
